package com.mlethe.library.recyclerview;

import androidx.annotation.NonNull;

import com.mlethe.library.recyclerview.callback.AbstractLoadCallback;
import com.mlethe.library.recyclerview.callback.Callback;

/**
 * 页面加载工具类
 *
 * @author Mlethe
 */
public class AdapterLoadSir {
    private static volatile AdapterLoadSir loadSir;
    private boolean isCommit;
    private Builder builder;

    public static AdapterLoadSir getDefault() {
        if (loadSir == null) {
            synchronized (AdapterLoadSir.class) {
                if (loadSir == null) {
                    loadSir = new AdapterLoadSir();
                }
            }
        }
        return loadSir;
    }

    private AdapterLoadSir() {
    }

    private void setBuilder(@NonNull Builder builder) {
        this.builder = builder;
    }

    public Builder getBuilder() {
        if (builder == null || !isCommit) {
            throw new IllegalArgumentException("The AdapterLoadSir.Builder is not commit.");
        }
        return builder;
    }

    /**
     * 是否提交
     *
     * @return true 已提交
     */
    public boolean isCommit() {
        return builder != null && isCommit;
    }

    public static Builder beginBuilder() {
        return new Builder();
    }

    public static class Builder {
        private Class<? extends Callback> defaultCallback;
        private Class<? extends AbstractLoadCallback> defaultLoadCallback;

        /**
         * 添加默认页面(推荐默认加载页面)
         *
         * @param defaultCallback 默认空视图
         * @return Builder
         */
        public Builder setDefaultCallback(@NonNull Class<? extends Callback> defaultCallback) {
            this.defaultCallback = defaultCallback;
            return this;
        }

        public Class<? extends Callback> getDefaultCallback() {
            return defaultCallback;
        }

        /**
         * 添加默认底部加载更多页面
         *
         * @param defaultCallback 默认底部加载更多页面
         * @return Builder
         */
        public Builder setDefaultLoadCallback(@NonNull Class<? extends AbstractLoadCallback> defaultCallback) {
            this.defaultLoadCallback = defaultCallback;
            return this;
        }

        public Class<? extends AbstractLoadCallback> getDefaultLoadCallback() {
            return defaultLoadCallback;
        }

        public void commit() {
            AdapterLoadSir loadSir = getDefault();
            loadSir.isCommit = true;
            loadSir.setBuilder(this);
        }
    }
}
